package com.live.player.openawardview;

import android.animation.ObjectAnimator;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

/**
 * 开奖号球 自下向上滚动
 */
public class OpenAwardCodeBall extends View {
    /**
     * 该View 的宽高  以px为单位
     */
    private int width = 64;
    private int height = 64;

    private Paint mTextPaint;
    private Paint.FontMetrics fontMetrics;
    private int count = 9;//默认是0-9这10个数字
    private String content = "?";
    private int maxHeight = 0;
    private int startIndex = 0;
    private long duration = 5000L;
    private ScollDirection scollDirection = ScollDirection.FORM_UP_TO_BOTTOM;//滚动方向  默认自上向下

    public OpenAwardCodeBall(Context context) {
        this(context, null);
    }

    public OpenAwardCodeBall(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public OpenAwardCodeBall(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setStyle(Paint.Style.STROKE);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setTextSize(45f);
        mTextPaint.setColor(getResources().getColor(R.color.color_white));
        fontMetrics = mTextPaint.getFontMetrics();
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.OpenAwardCodeBall);
        content = typedArray.getString(R.styleable.OpenAwardCodeBall_OpenAwardCodeBallContent);
        typedArray.recycle();
        maxHeight = count * height;
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(width, height);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = startIndex; i <= count; i++) {
            if (!content.matches("^[1-9]\\d*|0$")) {
                if (scollDirection.equals(ScollDirection.FORM_BOTTOM_TO_UP)) {
                    canvas.drawText(content, getWidth() / 2, ((getHeight() / 2 + Math.abs(fontMetrics.ascent - fontMetrics.descent) / 3) + i * getHeight()), mTextPaint);
                } else if (scollDirection.equals(ScollDirection.FORM_UP_TO_BOTTOM)) {
                    canvas.drawText(content, getWidth() / 2, ((getHeight() / 2 + Math.abs(fontMetrics.ascent - fontMetrics.descent) / 3) - i * getHeight()), mTextPaint);
                }
            } else {
                if (scollDirection.equals(ScollDirection.FORM_BOTTOM_TO_UP)) {
                    canvas.drawText(String.valueOf(i), getWidth() / 2, ((getHeight() / 2 + Math.abs(fontMetrics.ascent - fontMetrics.descent) / 3) + i * getHeight()), mTextPaint);
                } else if (scollDirection.equals(ScollDirection.FORM_UP_TO_BOTTOM)) {
                    canvas.drawText(String.valueOf(i), getWidth() / 2, ((getHeight() / 2 + Math.abs(fontMetrics.ascent - fontMetrics.descent) / 3) - i * getHeight()), mTextPaint);
                }
            }
        }
    }

    public ScollDirection getScollDirection() {
        return scollDirection;
    }

    public void setScollDirection(ScollDirection scollDirection) {
        this.scollDirection = scollDirection;
    }

    public void setStartToEndContent(int endContent) {
        setStartToEndContent(0, endContent);
    }

    /**
     * @param startContent 表示从该内容开始画文字
     * @param endContent   表示画到该内容截止
     */
    public void setStartToEndContent(int startContent, int endContent) {
        this.startIndex = startContent;
        this.count = endContent;
    }

    /**
     * 设置最终显示的内容
     * 显示的内容需要在开始数到结束数之间
     *
     * @param content
     */
    public void setContentAndRun(int content, long delayTime) {
        if (content >= 0 && content >= startIndex && content <= count) {
            this.content = String.valueOf(content);
            invalidate();
            startRun(delayTime);//开始滚动到设置的内容的位置
        } else {
            throw new IllegalArgumentException();
        }
    }

    /**
     * 设置默认的content内容  现默认显示?
     *
     * @param content
     */
    public void setDefaultContent(String content) {
        this.content = content;
    }


    public void setDuration(long duration) {
        this.duration = duration;
    }

    private void startRun(long delayTime) {
        ValueAnimator valueAnimator = null;

        valueAnimator = ObjectAnimator.ofObject(new TypeEvaluator() {
            @Override
            public Object evaluate(float fraction, Object startValue, Object endValue) {
                int startNum = (int) startValue;
                int endNum = (int) endValue;
                return startNum + (endNum - startNum) * fraction;
            }
        }, 0, count + Integer.valueOf(content)).setDuration(duration);
        valueAnimator.setStartDelay(delayTime);
        valueAnimator.setInterpolator(new DecelerateInterpolator());
        final ValueAnimator finalValueAnimator = valueAnimator;
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                if (value >= count) {
                    value = value % count;
                }
                //自上向下滚动
                if (scollDirection == ScollDirection.FORM_UP_TO_BOTTOM) {
                    scrollTo(0, (int) (value * -height));
                    if (Integer.valueOf(content) == value) {
                        scrollTo(0, (int) (value * -height));
                        finalValueAnimator.cancel();
                    }
                    //自下向上滚动
                } else if (scollDirection == ScollDirection.FORM_BOTTOM_TO_UP) {
                    scrollTo(0, (int) (value * height));
                    if (Integer.valueOf(content) == value) {
                        scrollTo(0, (int) (value * height));
                        finalValueAnimator.cancel();
                    }
                }
            }
        });
        valueAnimator.start();
    }
}
